﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDAOL.ModelView;
using QLDAOL.Util;
using QLDAOL.Models;
using XCaptcha.Web.Mvc;
using XCaptcha;
using QLDAOL.Captcha.Internal;
using QLDAOL.Email;
namespace QLDAOL.Controllers
{
    public class AccountController : BaseController
    {
        private IEQLDAOLRepository db = new IEQLDAOLRepository();
        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }
        
        [HttpGet]
        public ActionResult Logon()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Logon(LogonModel log)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            string user = log.username;
            string pass = Util.Util.GetMD5(log.password);
            var kt = db.ValidateUser(user, pass); 
            if (kt != null)
            {
                Session["user_id"] = kt.ID;
                Session["user_logon_name"] = kt.UserName;
                return View();

            }
            return View();
        }
        public ActionResult Logout()
        {
            Session["user_id"] = null;
            Session["user_logon_name"] = null;
            return RedirectToAction("Logon");
        }
        public int is_register(string user)
        {
            var kt = db.getUserByUsername(user);
            if (kt == null)
            {
                return 1;
            }
            return -1;
        }
        [HttpGet]
        public ActionResult Register()
        {

            RegisterModel reg = new RegisterModel();
            return View(reg);
        }
        [HttpPost]
        public ActionResult Register(RegisterModel reg)
        {
           string path = Server.MapPath("~/Email/template/active_template.htm");
            if (!ModelState.IsValid)
            {
                return View(reg);
            }
            var ht = db.getUserByUsername(reg.username);
            if (ht != null)
            {
                ModelState.AddModelError("Login", "Tên dăng nhập bị trùng");
                return View(reg);
            }
            string active = Util.Util.ramdomcode(15);
            string pass = "http://localhost:55357/account/active/" + active;
            string en_pass = Util.Util.GetMD5(reg.password);
            var newuser = new USER
            {
                UserName = reg.username,
                Password = en_pass,
                Email = reg.email,
                ID_SystemRole = 2
                //DM_Nganh=reg.job,
                //Age=int.Parse(reg.age),
               // ActiveCode = active,
              //  Status = 2

            };
           db.addUser(newuser);
            bool sentmail = EMailAddresss.send(path, newuser.Email, pass );
            return RedirectIndex(); ;
        }
        [DecryptSolution(Constants.SecretKey)]
        public ActionResult CaptchaImage(string solution)
        {


            //uncomment to use custom image
            //return new CaptchaImageResult(solution, new CustomCanvas(), new CustomTextStyle(), new CustomDistort(), new CustomNoise());

            return new CaptchaImageResult(solution);

        }

        [HttpGet]
        public string EncryptedCaptchaSolution()
        {
            var textGenerator = new XCaptcha.RandomTextGenerator();
            return textGenerator.CreateRandomUrlEncodedEncrypedText(Constants.SecretKey, 4);



        }
    }
}
